<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
 */

class topic_api {
	
	public function delete($id){
		$topicdb = load_db("topic");
		$condition['topic_id'] = $id;
		$result = load_db("topic/favtopic")->delete($condition);
		$result = load_db("topic")->delete($condition);
		$banner_info = banner_path($id);
		$banner_dir = BASEPATH.$banner_info['dir'];
		file_exists($banner_dir.$banner_info['filename']) && unlink($banner_dir.$banner_info['filename']);
		return $result;
	}
	
	public function searchFull($conditions,$page,$pagesize,$noarticle=true){
		$topics = load_db("topic")->search($conditions,$page,$pagesize,null,null);
		if($topics){
			$topics = $this->prepareTopics($topics,$noarticle);
		}
		return $topics;
	}
	
	public function prepareTopics($topics,$noarticle=true){
		$shareapi = load_api('share');
		$pager = &load_class('Pager', 'core');
		foreach ($topics as $key=>$topic){
			$share_conditions['keyword'] = 'topic'.pzxencode($topic['topic_title']).'t';
			if($noarticle) $share_conditions['type'] = 'noarticle';
			$shares = $shareapi->search($share_conditions,1,7,null);
			$topics[$key]['shares'] = $shareapi->prepareShares($shares);
			$pageData = $pager->getPager();
			$topics[$key]['numbershares'] = $pageData['total_count'];
			$topics[$key]['keywords'] = ($topic['keyword'])?encodekeywordsarray(explode(',', $topic['keyword'])):null;
			$topics[$key]['banner'] =  topicbanner($topic['topic_id']);
			$topics[$key]['bannercrop'] =  thumbpath(topicbanner($topic['topic_id']),'crop');
			$topics[$key]['bannercover'] =  thumbpath(topicbanner($topic['topic_id']),'cover');
		}
		
		return $topics;
	}
	
	public function gettopicnumbyuid($uid){
		return load_db('topic')->findCount(array('user_id'=>$uid));
	}
}

